package com.fjnuvg.videoplayer.dyc;

import java.io.IOException;

import io.vov.vitamio.MediaPlayer;

import com.fjnuvg.videoplayer.R;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PlayerActivity extends Activity implements OnClickListener,SurfaceHolder.Callback,MediaPlayer.OnBufferingUpdateListener,MediaPlayer.OnPreparedListener,MediaPlayer.OnVideoSizeChangedListener,MediaPlayer.OnCompletionListener,MediaPlayer.OnErrorListener
{
	SurfaceView playerView=null;
	Button start,pause,stop;
	SurfaceHolder holder;
	MediaPlayer mediaPlayer=null;
	String path="";
	int height,width;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player);
		path=getIntent().getStringExtra("path");
		playerView=(SurfaceView)findViewById(R.id.play_feild);
		start=(Button)findViewById(R.id.play);
		stop=(Button)findViewById(R.id.stop);
		pause=(Button)findViewById(R.id.pause);
		holder=playerView.getHolder();
		holder.setFormat(PixelFormat.RGBA_8888);
		holder.addCallback(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String vtag=(String)v.getTag();
		if (vtag.equals("start")) {
			if (mediaPlayer!=null) {
				mediaPlayer.start();
				return;
			}
			try{
			playVideo();
			}catch(Exception exception)
			{
				exception.printStackTrace();
			}
			return;
			
		}
		if (vtag.equals("pause")) {
			if (mediaPlayer!=null) {
				if (mediaPlayer.isPlaying()) {
					mediaPlayer.pause();
				}
			}
			return;
		}
		if (vtag.equals("stop")) {
			if (mediaPlayer!=null) {
				mediaPlayer.stop();
			}
		}
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		try{
		playVideo();
		}catch(Exception e)
		{
			e.printStackTrace();
			return;
		}
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	private void playVideo() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
		if (path.trim().equals("")) {
			return;
		}
		mediaPlayer=new MediaPlayer(PlayerActivity.this);
		mediaPlayer.setDisplay(holder);
		mediaPlayer.setDataSource(path);
		mediaPlayer.setOnBufferingUpdateListener(this);
		mediaPlayer.setOnPreparedListener(this);
		mediaPlayer.setOnErrorListener(this);
		mediaPlayer.setOnCompletionListener(this);
		mediaPlayer.setOnVideoSizeChangedListener(this);
		mediaPlayer.prepareAsync();
		
	}
	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onCompletion(MediaPlayer mp) {
		// TODO Auto-generated method stub
		mp.stop();
		
	}
	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		Log.e("PlayerActivity-error", "what="+String.valueOf(what)+" extra="+String.valueOf(extra));
		
		return false;
	}
	@Override
	public void onPrepared(MediaPlayer mp) {
		// TODO Auto-generated method stub
		startPlay();
		
	}
	@Override
	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
		// TODO Auto-generated method stub
		if (width==0||height==0) {
			Log.e("PlayerActivity-OnVideoSizeChanged", "invalid size!!");
			return;
		}
		this.height=height;
		this.width=width;
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (mediaPlayer!=null) {
			mediaPlayer.release();
		}
		height=0;
		width=0;
		super.onDestroy();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (mediaPlayer!=null) {
			mediaPlayer.release();
		}
		height=0;
		width=0;
		super.onPause();
	}
	private void startPlay() {
		holder.setFixedSize(width, height);
		mediaPlayer.start();
	}
	
	
}
